/****************************************************
 * 创建人:   robin
 * 创建时间: 2023/8/30.030 11:00
 * 项目名称: cpy
 * 文件名称: TaskThreadPool.java
 * 文件描述:
 *
 * All rights Reserved, Designed By 德品软件
 * @Copyright:2023-2023
 *
 ********************************************************/
package com.dp.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 包名称：com.dp.utils
 * 类名称：TaskThreadPool
 * 类描述：
 * 创建人： robin
 * 创建时间：2023/8/30.030 11:00
 */
@Slf4j
public class TaskThreadPool {
    private static TaskThreadPool taskThreadPool = null;
    private ExecutorService executorService;
    private TaskThreadPool() {
        this.executorService = Executors.newCachedThreadPool();
    }

    public static <T> Future<T> add(Callable<T> task) {
        if(null == taskThreadPool) {
            synchronized (TaskThreadPool.class) {
                if(null == taskThreadPool) {
                    taskThreadPool = new TaskThreadPool();
                }
            }
        }
        return taskThreadPool.executorService.submit(task);
    }

    public static void closeThreadPool() {
        if(taskThreadPool != null) {
            synchronized (TaskThreadPool.class) {
                if(taskThreadPool != null) {
                    taskThreadPool.executorService.shutdown();
                    taskThreadPool = null;
                }
            }
        }
    }
    
    public static void printStatus() {
        if(taskThreadPool != null) {
            synchronized (TaskThreadPool.class) {
                if(taskThreadPool != null) {
                    log.info("TaskThreadPool status:{}", taskThreadPool.executorService);
                }
            }
        }
    }
}
